using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            valPostCodeFormat.ValidationExpression = School.PostCodeValidationExpression;
            valEmailAddressFormat.ValidationExpression = School.EmailValidationExpression;
            valPasswordFormat.ValidationExpression = School.PasswordValidationExpression;
        }

    }
    protected void btnEnter_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            School tmp = new School();
            tmp.SchoolName = txtSchoolName.Text.Trim();
            try
            {
                tmp.SchoolType = (School.SchoolTypes)Enum.Parse(typeof(School.SchoolTypes), ddlType.SelectedValue);
            }
            catch { }
            tmp.AddressLine1 = txtAddress1.Text.Trim();
            tmp.AddressLine2 = txtAddress2.Text.Trim();
            tmp.AddressLine3 = txtAddress3.Text.Trim();
            tmp.Town = txtTown.Text.Trim();
            tmp.County = txtCounty.Text.Trim();
            tmp.PostCode = txtPostCode.Text.Trim();
            tmp.SchoolTel = txtSchoolTel.Text.Trim();
            tmp.ContactFirstName = txtContactFirstName.Text.Trim();
            tmp.ContactLastName = txtContactLastName.Text.Trim();
            tmp.ContactEmailAddress = txtEmailAddress.Text.Trim();
            tmp.ContactTel = txtContactTel.Text.Trim();
            tmp.SecondaryContactName = txtSecondaryContact.Text.Trim();
            tmp.Password = txtPassword.Text.Trim();

            try
            {
                tmp.Save();
            }
            catch (ValidationException ex)
            {
                System.Text.StringBuilder messages = new System.Text.StringBuilder();
                foreach (ValidationExceptionItem item in ex.Items)
                {
                    messages.AppendFormat("<li>{0}</li>", item.Message);
                }
                pnlErrors.Visible = true;
                ulErrors.InnerHtml = messages.ToString();
                return;
            }

            //Send the email
            Acms.Bll.Email email = Acms.Bll.Email.CreateInstance(tmp.ContactDisplayName, tmp.ContactEmailAddress, Server.MapPath("~/Emails/SchoolRegisterdEmail.xml"));
            email.Format("SchoolName", tmp.SchoolName);
            email.Send();

            pnlForm.Visible = false;
            pnlEntry.Visible = true;

        }
    }
}
